import type { IMenuState, IAsyncMenuState } from '@/types/user/menu';
const modules = import.meta.glob('../views/**/*.vue');

// 第一层级
const layoutFrist = () => import('@/page/layout/index.vue');
// 中间层级
const layoutCenter = () => import('@/page/layout/layout.vue');

// 最后层级
function loadComponent(path: string) {
    const target = modules[`../views${path}.vue`];
    return target || modules[`../views/error/index.vue`];
}

export const handleMenuList = (menuList: Array<IAsyncMenuState>, isFirstLevel = true) => {
    return menuList.map((item: IAsyncMenuState) => {
        const isLastLevel = !item.children || item.children.length === 0;
        const menu: IMenuState = {
            name: item.name,
            path: item.path,
            icon: item.source,
            redirect: '',
            children: item.children ? handleMenuList(item.children, false) : [],
            component: isFirstLevel ? layoutFrist : isLastLevel ? loadComponent(item.path) : layoutCenter
        };
        return menu;
    });
};
